﻿<Tree ClickToggleNode="true" Items="@GetLazyItems()" OnExpandNode="OnExpandNode" />

@code {
    private static List<TreeItem> GetLazyItems()
    {
        var ret = TreeDataFoo.GetTreeItems();
        ret[1].Items[0].IsCollapsed = false;
        ret[1].Items[1].Text = "lazy loading";
        ret[1].Items[1].HasChildNode = true;
        ret[1].Items[2].Text = "lazy loading delay";
        ret[1].Items[2].HasChildNode = true;
        ret[1].Items[2].Key = "Delay";

        return ret;
    }

    private static async Task OnExpandNode(TreeItem item)
    {
        if (!item.Items.Any() && item.HasChildNode && !item.ShowLoading)
        {
            item.ShowLoading = true;
            if (item.Key?.ToString() == "Delay")
            {
                await Task.Delay(800);
            }
            item.Items.AddRange(new TreeItem[]
            {
                    new TreeItem()
                    {
                        Text = "lazy loading child node 1",
                        HasChildNode = true
                    },
                    new TreeItem() { Text = "lazy loading child node 2" }
            });
            item.ShowLoading = false;
        }
    }
}
